<script lang="ts" setup>
import { computed, unref } from 'vue'

import { Layout } from 'ant-design-vue'
import LayoutTrigger from '../trigger/index.vue'
import { ErrorAction, LayoutBreadcrumb, UserDropDown } from './components'
import { propTypes } from '@/utils/propTypes'

import { AppSizePicker } from '@/components/Application'

import { useHeaderSetting } from '@/hooks/setting/useHeaderSetting'
import { useRootSetting } from '@/hooks/setting/useRootSetting'

import { SettingButtonPositionEnum } from '@/enums/appEnum'

import { useAppInject } from '@/hooks/web/useAppInject'
import { useDesign } from '@/hooks/web/useDesign'

import { createAsyncComponent } from '@/utils/factory/createAsyncComponent'

defineOptions({ name: 'LayoutHeader' })
const props = defineProps({
  fixed: propTypes.bool,
})
const Header = Layout.Header
const SettingDrawer = createAsyncComponent(() => import('@/layouts/default/setting/index.vue'), {
  loading: true,
})
const { prefixCls } = useDesign('layout-header')
const { getUseErrorHandle, getShowSettingButton, getSettingButtonPosition } = useRootSetting()

const { getShowHeader } = useHeaderSetting()

const { getIsMobile } = useAppInject()

const getHeaderClass = computed(() => {
  return [
    prefixCls,
    {
      [`${prefixCls}--fixed`]: props.fixed,
      [`${prefixCls}--mobile`]: unref(getIsMobile),
      [`${prefixCls}--light`]: true,
    },
  ]
})

const getShowSetting = computed(() => {
  if (!unref(getShowSettingButton))
    return false

  const settingButtonPosition = unref(getSettingButtonPosition)

  if (settingButtonPosition === SettingButtonPositionEnum.AUTO)
    return unref(getShowHeader)

  return settingButtonPosition === SettingButtonPositionEnum.HEADER
})
</script>

<template>
  <Header :class="getHeaderClass">
    <!-- left start -->
    <div :class="`${prefixCls}-left`">
      <LayoutTrigger />
      <LayoutBreadcrumb />
    </div>
    <!-- left end -->

    <!-- action  -->
    <div :class="`${prefixCls}-action`">
      <ErrorAction v-if="getUseErrorHandle" :class="`${prefixCls}-action__item error-action`" />

      <AppSizePicker :show-text="false" :class="`${prefixCls}-action__item`" />

      <UserDropDown />

      <SettingDrawer v-if="getShowSetting" :class="`${prefixCls}-action__item`" />
    </div>
  </Header>
</template>

<style lang="less">
@import './index.less';
</style>
